<template>
  <div id="userLogin">
    <h2>注册 OJ 在线判题系统</h2>
    <a-form
      :model="form"
      auto-label-width
      label-align="left"
      style="max-width: 480px; margin: 0 auto"
      @submit-success="handleSubmit"
    >
      <a-form-item
        :rules="[{ required: true, message: '账号不能为空' }]"
        field="userAccount"
        label="账号"
      >
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码不能少于 8 位' },
        ]"
        field="userPassword"
        label="密码"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
          autocomplete="off"
        />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '确认密码不能为空' },
          { minLength: 8, message: '确认密码不能少于 8 位' },
        ]"
        field="checkPassword"
        label="确认密码"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请再次输入密码"
          autocomplete="off"
        />
      </a-form-item>
      <a-form-item>
        <a-space align="end" style="flex: 1; justify-content: space-between">
          <a-link class="login" href="/user/login">已有账号，去登录</a-link>
          <a-button
            class="register"
            html-type="submit"
            shape="round"
            type="primary"
          >
            注册
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { UserControllerService } from "../../../generated";
import { useRouter } from "vue-router";
import { Message } from "@arco-design/web-vue";

const router = useRouter();

// 登录表单数据
const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
});

// 处理注册
const handleSubmit = async () => {
  // 发生注册请求
  const res = await UserControllerService.userRegisterUsingPost(form);
  if (res.code === 0) {
    // 注册成功，跳转到登录页
    Message.success("注册成功，自动跳转登录中~");
    await router.push({ path: "/user/login" });
  } else {
    // 注册失败，提示用户失败原因
    Message.error("注册失败，" + res.message);
  }
};
</script>

<style scoped>
/*注册*/
.register {
  width: 120px;
}

/*登录账号*/
.login {
  //margin-left: 36px;
}
</style>